import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
const server = 'http://www.bit000.com:5000' // 正式环境

// const server = 'http://192.168.123.214:5000' // 本地环境

// const server = 'http://39.104.191.49:5000'   // 测试环境

axios.interceptors.request.use(function (config) {
  config.headers.Authorization = `Bearer ${window.localStorage.getItem('Authorization')}`
  return config
}, function (error) {
  return Promise.reject(error)
})

const http = {
  async get(url, data) {
    try {
      const res = await axios.get(server + url);
      if (res.data.status == 401) {
        location.hash = "/login";
        return false;
      }
      return res
    } catch (e) {
      console.log(e)
    }
  },
  async put(url, param) {
    try {
      const res = await axios.put(server + url, qs.stringify(param));
      if (res.data.status == 401) {
        location.hash = "/login";
        return false;
      }
      return res
    } catch (e) {
      console.log(e)
    }
  },
  async post(url, param) {
    try {
      const res = await axios.post(server + url, qs.stringify(param));
      if (res.data.status == 401) {
        location.hash = "/login";
        return false;
      }
      return res
    } catch (e) {
      console.log(e)
    }
  },
  async update(url, param) {
    try {
      const res = await axios.update(server + url, qs.stringify(param));
      if (res.data.status == 401) {
        location.hash = "/login";
        return false;
      }
      return res
    } catch (e) {
      console.log(e)
    }
  },
  async delete(url) {
    try {
      const res = await axios.delete(server + url);
      if (res.data.status == 401) {
        location.hash = "/login";
        return false;
      }
      return res
    } catch (e) {
      console.log(e)
    }
  }
}

export default http
